package org.herrdommel.wakemeup.model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.herrdommel.wakemeup.R;
import org.herrdommel.wakemeup.WakeMeUpApplication;

import android.content.Context;
import android.database.Cursor;
import android.widget.TimePicker;

public class Alarm {

	// Database fields
	public static final String ALARMS_TABLE_NAME = "tableAlarms";
	public static final String _ID = "_id";
	public static final String NAME = "name";
	public static final String TIME = "time";
	public static final String IS_ACTIVE = "isactive";
	public static final String NEEDS_CONFIRMATION = "needsconfirmation";
	public static final String ALERT_ON_LOW_BATTERY = "alertonlowbattery";

	/** SimpleDateFormat for persisting the time to the database and shared preferences */
	public static final SimpleDateFormat SDF = new SimpleDateFormat("HH:mm");

	/** The alarms id */
	private long id;
	/** The alarms name */
	private String name;
	/** The alarms time to set of */
	private String time;
	/** The flag to indicate if the alarm is active */
	private boolean isActive;
	/** The flag to indicate if the alarm needs confirmation */
	private boolean needsConfirmation;
	/** The flag to indicate if the alarm sets off on low battery */
	private boolean alertOnLowBattery;

	/** Default constructor, creating an alarm and filling it with default values */
	public Alarm() {
		super();
		setDefaultValue();
	}

	/** Constructor using a {@link Cursor} pointing to a alarm row to create an alarm */
	public Alarm(final Cursor c) {
		this();
		setId(c.getLong(c.getColumnIndex(Alarm._ID)));
		setName(c.getString(c.getColumnIndex(Alarm.NAME)));
		setAlarmTime(c.getString(c.getColumnIndex(Alarm.TIME)));
		setActive(c.getInt(c.getColumnIndex(Alarm.IS_ACTIVE)) > 0);
		setNeedsConfirmation(c.getInt(c.getColumnIndex(Alarm.NEEDS_CONFIRMATION)) > 0);
		setAlertOnLowBattery(c.getInt(c.getColumnIndex(Alarm.ALERT_ON_LOW_BATTERY)) > 0);
	}

	public long getId() {
		return id;
	}

	public void setId(final long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(final String name) {
		this.name = name;
	}

	public String getAlarmTime() {
		return time;
	}

	public Date getAlarmTimeAsDate() {
		try {
			return SDF.parse(this.time);
		} catch (final ParseException e) {
			return null;
		}
	}

	public void setAlarmTime(final String alarmTime) {
		this.time = alarmTime;
	}

	public void setAlarmTimeFromTimePicker(final TimePicker tp) {
		final Calendar c = Calendar.getInstance();
		c.set(Calendar.HOUR_OF_DAY, tp.getCurrentHour());
		c.set(Calendar.MINUTE, tp.getCurrentMinute());
		this.time = SDF.format(c.getTime());
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(final boolean isActive) {
		this.isActive = isActive;
	}

	public boolean isNeedsConfirmation() {
		return needsConfirmation;
	}

	public void setNeedsConfirmation(final boolean needsConfirmation) {
		this.needsConfirmation = needsConfirmation;
	}

	public boolean isAlertOnLowBattery() {
		return alertOnLowBattery;
	}

	public void setAlertOnLowBattery(final boolean alertOnLowBattery) {
		this.alertOnLowBattery = alertOnLowBattery;
	}

	/** Sets all values to default */
	private void setDefaultValue() {
		final Context c = WakeMeUpApplication.getWakeMeUpContext();
		this.id = -1;
		this.name = c.getString(R.string.prefDefaultName);
		this.time = SDF.format(new Date());
		this.isActive = true;
		this.needsConfirmation = false;
		this.alertOnLowBattery = true;
	}

}
